import { defineStore } from 'pinia';
import { ref } from 'vue';
import { myTask } from '@/api/mock/task/type';
import { addTask, delTask, editTask, getTaskList } from '@/api/mock';

const useTestTaskStore = defineStore('testTasks', () => {
  const taskList = ref<myTask[]>([]);

  //   拉取任务列表
  const initTaskList = async () => {
    const res = await getTaskList();
    taskList.value = res;
  };

  //   新建任务
  const createTask = async (task: myTask) => {
    const res = (await addTask(task)) as myTask;
    console.log('create', res);
    taskList.value.push(res);
  };
  //   编辑任务
  const updateTask = async (task: myTask) => {
    const res = await editTask(task);
    console.log('update', res);
    // 重新拉取任务列表
    initTaskList();
  };
  //   删除任务
  const deleteTask = async (id: string) => {
    const res = await delTask(id);
    console.log('delete', res);
    taskList.value = taskList.value.filter((item) => item.id !== id);
  };
  return {
    taskList,
    initTaskList,
    createTask,
    updateTask,
    deleteTask,
  };
});
export default useTestTaskStore;
